﻿using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Sw_MyAddin.SLDASM
{
    class Batch_Component
    {
        /// <summary>
        /// 遍历零部件并做操作
        /// </summary>
        /// <param name="swApp"></param>
        public static void Function(ISldWorks swApp)
        {
            Check_ExcludeFromBOM(swApp);
        }
        private static string Select_nema(Component2 Com)//递归
        {
            string s = null;
            while (Com.GetParent() != null)
            {
                Select_nema(Com);
                Console.WriteLine(s);
            }
            return s;
        }


        //————  功能  ————//
        public static void Set_lightwight(ISldWorks swApp)//轻化
        {
            // 获取当前活动的文档
            ModelDoc2 model = swApp.ActiveDoc;
            AssemblyDoc swAssy = (AssemblyDoc)model;

            // 获取零部件，true仅获取顶层组件，false是全部 //vComponents遍历零部件
            object[] vComponents = swAssy.GetComponents(false);
            //进度条
            进度条 form1 = new 进度条(); form1.Show();
            form1.progressBar1.Value = 0; ;
            form1.progressBar1.Maximum = vComponents.Length;

            swAssy.ResolveAllLightweight();

            int i = 0;
            foreach (Component2 SingleComponent in vComponents)
            {
                //找到还原零件               
                ////SingleComponent
                //进度条
                form1.progressBar1.Value += 1; i += 1;
                form1.数字显示(i, vComponents.Length);
            }
        }
        public static void Set_Virtual(ISldWorks swApp)//虚拟化+排除BOM+移动零部件
        {
            List<string> list1 = new List<string>(); //记录需要移动的外部零件
            List<string> list2 = new List<string>(); //记录需要移动的外部零件
            // 获取当前活动的文档
            ModelDoc2 swDoc = swApp.ActiveDoc;
            AssemblyDoc swAssy = (AssemblyDoc)swDoc;

            // 获取零部件，true仅获取顶层组件，false是全部 //vComponents遍历零部件
            object[] vComponents = swAssy.GetComponents(false);
            //swAssy.ResolveAllLightweight();

            if (vComponents != null)
            {
                //添加内容
                StreamWriter sw = new StreamWriter(@"C:\Windows\Temp\logfile_Virtual.txt", true, System.Text.Encoding.GetEncoding("gb2312"));
                foreach (Component2 SingleComponent in vComponents)
                {
                    //记录需要转移的零部件
                    list1.Add(SingleComponent.GetPathName());
                    sw.WriteLine(SingleComponent.GetPathName());
                    //虚拟化零部件              
                    SingleComponent.MakeVirtual();
                    SingleComponent.Name2 = SingleComponent.Name2.Replace("复件 ", "");
                    SingleComponent.ExcludeFromBOM = true;
                }

                sw.Flush();
                sw.Close();
            }

            //释放零部件内存
            swDoc.Save(); swApp.CloseDoc(swDoc.GetPathName());

            ////数组去重
            //list1.Distinct().ToArray();
            //for (int i = 0; i < list1.Count; i++)
            //{
            //    list2.Add(list1[i].Replace("A0-校准件库", "A0-校准件库(虚拟化备份)"));
            //    string s = Path.GetDirectoryName(list2[i]);
            //    if (Directory.Exists(s))
            //    {
            //        if (File.Exists(list1[i])) { File.Move(list1[i], list2[i]); }
            //    }
            //    else
            //    {
            //        DirectoryInfo directoryInfo = new DirectoryInfo(s); directoryInfo.Create();
            //        if (File.Exists(list1[i])) { File.Move(list1[i], list2[i]); }
            //    }
            //}
        }
        public static void Sync_PropToconfig(ISldWorks swApp)//自定义属性同步到所有配置特定属性
        {
            // 获取当前活动的文档
            ModelDoc2 model = swApp.ActiveDoc;
            AssemblyDoc swAssy = (AssemblyDoc)model;
            // 写入属性之前，将装配体设定轻化到还原
            //swAssy.ResolveAllLightWeightComponents(true);

            // 获取零部件，true仅获取顶层组件，false是全部 //vComponents遍历零部件
            object[] vComponents = swAssy.GetComponents(false);
            //进度条
            进度条 form1 = new 进度条(); form1.Show();
            form1.progressBar1.Value = 0; ;
            form1.progressBar1.Maximum = vComponents.Length;

            int i = 0;
            foreach (Component2 SingleComponent in vComponents)
            {
                //获得特征,找到基准面特征
                Feature swFeat = (Feature)SingleComponent.FirstFeature();
                while ((swFeat != null))
                {
                    if ("RefPlane" == swFeat.GetTypeName() && swFeat.Visible == 2)
                    {
                        string s;//装配体选择对象的名称会有层级划分：“特征名@层级名@总装名”
                        model.Extension.SelectByID2(swFeat.GetNameForSelection(out s), "PLANE", 0, 0, 0, false, 0, null, 0);
                        //隐藏对象
                        model.BlankRefGeom();
                    }
                    swFeat = (Feature)swFeat.GetNextFeature();
                }
                //进度条
                form1.progressBar1.Value += 1; i += 1;
                form1.数字显示(i, vComponents.Length);
            }
        }
        public static void Check_ExcludeFromBOM(ISldWorks swApp)//检查不包括在材料明细表的属性设置，并恢复成包括
        {
            // 获取当前活动的文档
            ModelDoc2 model = swApp.ActiveDoc;
            AssemblyDoc swAssy = (AssemblyDoc)model;
            // 写入属性之前，将装配体设定轻化到还原
            //swAssy.ResolveAllLightWeightComponents(true);

            // 获取零部件，true仅获取顶层组件，false是全部 //vComponents遍历零部件
            object[] vComponents = swAssy.GetComponents(false);
            //进度条
            进度条 form1 = new 进度条(); form1.Show();
            form1.progressBar1.Value = 0; ;
            form1.progressBar1.Maximum = vComponents.Length;

            bool Switch_ExcludeFromBOM = false;
            int i = 0;
            foreach (Component2 SingleComponent in vComponents)
            {
                //虚拟化零部件
                if (SingleComponent.ExcludeFromBOM == true) { Switch_ExcludeFromBOM = true; }
                SingleComponent.ExcludeFromBOM = false;

                //进度条
                form1.progressBar1.Value += 1; i += 1;
                form1.数字显示(i, vComponents.Length);
            }
            if (Switch_ExcludeFromBOM) { MessageBox.Show("存在不包括在材料明细表中的零部件,已自动修正"); }

        }



        /// <summary>
        /// 选中零部件计数
        /// </summary>
        /// <param name="swApp"></param>
        public static void GetSelectedMember(ISldWorks swApp)
        {
            ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc;
            if (swDoc != null)
            {
                SelectionMgr swSelMgr = (SelectionMgr)swDoc.SelectionManager; //选择管理器对象

                //获取SOLIDWORKS主框架。
                Frame swFrame = (Frame)swApp.Frame();
                //在状态栏左侧的主状态栏区域中显示文本字符串。
                //GetSelectedObjectCount2(-1)方法为获取被选择对象的数量,-1表示所有对象
                swFrame.SetStatusBarText("选择了 " + swSelMgr.GetSelectedObjectCount2(-1) + " 个零部件");
                MessageBox.Show("选择了【 " + swSelMgr.GetSelectedObjectCount2(-1) + " 】个零部件");
            }
            else
            {
                MessageBox.Show("请打开装配体");
            }
        }


        //—————— 记录虚拟件零件路径，用于移到 ——————//
        public static void Creatlogfile(string text)    //新建日志文件
        {
            ////清空内容
            //FileStream fs = new FileStream(@"C:\Windows\Temp\logfile.txt", FileMode.OpenOrCreate);
            //fs.Seek(0, SeekOrigin.Begin);
            //fs.SetLength(0);
            //fs.Close();

            //添加内容
            StreamWriter sw = new StreamWriter(@"C:\Windows\Temp\logfile.txt", true, System.Text.Encoding.GetEncoding("gb2312"));
            sw.WriteLine(text);
            sw.Flush();
            sw.Close();
        }
        public static void Openlogfile()    //打开日志文件
        {
            //File.OpenText(@"C:\Windows\Temp\logfile.txt");
            //System.Diagnostics.Process.Start(@"C:\Windows\Temp\logfile.txt");
        }

    }
}
